package scripts;
import javax.swing.JOptionPane;
import bot.ScriptMethods;

public class HighAlchemy extends ScriptMethods implements Runnable {

	private int alchItem;

	public void start() {
		System.gc();
		getAccountSettings();

		String id = JOptionPane.showInputDialog("Please enter the item ID to cast High Alchemy on: "); 
		alchItem = Integer.parseInt(id);

		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		scriptRunning = false;
		scriptThread = null;
		System.gc();
		debug("Stopped script.");
	}

	public void run() {
		while (scriptRunning) {
			try {
				handleEvents();
				if (bankOpen()) {
					if (!invContains(alchItem)) {
						withdraw(alchItem, 25);
						wait(random(600, 750));
					} else {
						closeBank();
						wait(random(600, 750));
					}
				}
				if (!invContains(alchItem)) {
					if (!bankOpen()) {
						openBank();
						wait(2500);
					}
					wait(100);
				} else {
					if (!isSleeping()) {
						highAlchItem(alchItem);
						wait(random(800, 1200));
					}
				}
				wait(100);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 80 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
	}
}